package com.code.wflow.service.impl;

import com.code.wflow.mapper.InstanceNodeMapper;
import com.code.wflow.model.InstanceNode;
import com.code.wflow.service.InstanceNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 流程实例节点表(WorkflowInstanceNode)表服务层
 *
 * @author ggh
 * @date 2022/11/10
 */
@Service
public class InstanceNodeServiceImpl implements InstanceNodeService {
    @Autowired
    private InstanceNodeMapper instanceNodeMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public InstanceNode queryById(Long id) {
        return this.instanceNodeMapper.queryById(id);
    }

    /**
     * 查询下一个节点
     *
     * @param instanceId
     * @param level
     * @return
     */
    @Override
    public InstanceNode queryNodeByLevel(Long instanceId, Integer level) {
        return this.instanceNodeMapper.queryNodeByLevel(instanceId, level);
    }

    /**
     * 查询相同条件下的下一个节点
     *
     * @param instanceId
     * @param level
     * @return
     */
    @Override
    public InstanceNode queryNextNodeByCondition(Long instanceId, Integer level, Integer conditionsLevel, String parentCode) {
        return this.instanceNodeMapper.queryNextNodeByCondition(instanceId, level, conditionsLevel, parentCode);
    }


    /**
     * 查询所有节点
     *
     * @param instanceId
     * @return
     */
    @Override
    public List<InstanceNode> queryInstanceNode(Long instanceId) {
        return this.instanceNodeMapper.queryInstanceNode(instanceId);
    }

    /**
     * 修改数据
     *
     * @param workflowInstanceNode 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(InstanceNode workflowInstanceNode) {
        return this.instanceNodeMapper.update(workflowInstanceNode) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.instanceNodeMapper.deleteById(id) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param idList 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIdList(List<Long> idList) {
        if (this.instanceNodeMapper.deleteByIdList(idList) == idList.size()) {
            return true;
        }
        throw new RuntimeException();
    }
}
